package foodmaterial.action;

import foodmaterial.config.DataSourceContextHolder;
import foodmaterial.dto.PermissionDto;
import foodmaterial.entity.*;
import foodmaterial.service.InitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@RestController
@RequestMapping("/api/init")
@CrossOrigin(allowCredentials = "true")
public class InitAction extends BaseAction {

    @Autowired
    private InitService service ;

    @PostMapping("/addCompanyDatabase")
    public JsonResult<String> addCompanyDatabase(@RequestBody CompanyDatabase companyDatabase) throws SQLException, IOException, ClassNotFoundException {
        String result = service.addCompanyDatabase(companyDatabase) ;
        return result == null ? new JsonResult( true,"新增公司成功"):new JsonResult( false,result) ;
    }

    @PostMapping("/modifyCompanyDatabase")
    public JsonResult<String> modifyCompanyDatabase(@RequestBody CompanyDatabase companyDatabase) {
        int result = service.modifyCompanyDatabase(companyDatabase) ;
        return result > 0 ? new JsonResult( true,"修改公司成功"):new JsonResult( false,"修改公司失败") ;
    }

    @GetMapping("/queryCompanyDatabase")
    public JsonResult<List<CompanyDatabase>> queryCompanyDatabase() {
        List<CompanyDatabase> data = service.queryCompanyDatabase() ;
        return new JsonResult(true, ResultCode.SUCCESS,data) ;
    }

    @GetMapping("/showCompany")
    public JsonResult<List<CompanyDatabase>> showCompanyDatabase() {
        List<CompanyDatabase> data = service.queryCompanyDatabase() ;
        return new JsonResult(true, ResultCode.SUCCESS,data) ;
    }

    @PostMapping("/modifySysParams")
    public JsonResult<String> modifySysParams(@RequestBody SysParams sysParams) {
        int result = service.modifySysParams(sysParams) ;
        return result > 0 ? new JsonResult( true,"修改参数成功"):
                new JsonResult( false,"修改参数失败") ;
    }

    @GetMapping("/querySysParams")
    public JsonResult<List<SysParams>> querySysParams() {
        List<SysParams> data = service.querySysParams() ;
        return new JsonResult(true, ResultCode.SUCCESS,data) ;
    }

    @GetMapping("/querySysParamsByCode")
    public JsonResult<SysParams> querySysParamsByCode(@RequestParam("code") String code) {
        SysParams result = service.querySysParamsByCode(code) ;
        return new JsonResult(true,ResultCode.SUCCESS,result) ;
    }

    @GetMapping("/querySysParamsByCodes")
    public JsonResult<List<SysParams>> querySysParamsByCodes(@RequestParam("codes") List<String> codes) {
        List<SysParams> result = service.querySysParamsByCodes(codes) ;
        return new JsonResult(true,ResultCode.SUCCESS,result) ;
    }

    @GetMapping("/getCompanyUseDays")
    public JsonResult<Integer> getCompanyUseDays() {
        Integer integer = service.getCompanyUseDays() ;
        return new JsonResult<>(true,ResultCode.SUCCESS,integer) ;
    }

    @GetMapping("/queryCompanyFun")
    public JsonResult<String> queryCompanyFun(@RequestParam("database") String database) {
        DataSourceContextHolder.setDBType(database);
        try {
            String result = service.queryCompanyFun();
            return new JsonResult<>(true, ResultCode.SUCCESS, result);
        } finally {
            DataSourceContextHolder.clearDBType();
        }
    }

    @PostMapping("/initCompanyPermission")
    public JsonResult<String> initCompanyPermission(@RequestBody PermissionDto dto) {
        DataSourceContextHolder.setDBType(dto.getDatabase().trim());
        try {
            String result = service.initCompanyPermission(dto);
            return result == null ? new JsonResult(true, "设置成功") :
                    new JsonResult(false, result);
        } finally {
            DataSourceContextHolder.clearDBType();
        }
    }
}
